package com.lagou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.mapper.GoodsMapper;
import com.lagou.pojo.Goods;
import com.lagou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    //通过id查询商品
    @Override
    public Goods findGoodsById(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        return goods;
    }

    //通过id删除商品
    @Override
    public int deleteGoodsById(Integer id) {
        int i = goodsMapper.deleteById(id);
        return i;
    }

    //通过id修改商品
    @Override
    public int updateGoodsById(Goods goods) {

        int i = goodsMapper.updateById(goods);
        return i;
    }

    //分页查询
    @Override
    public Map<List,Integer> selectGoodsByPage(Integer page, String name, String classify, Integer pricemin, Integer pricemax, Integer inventorymin, Integer inventorymax, String status) {
        QueryWrapper<Goods> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper
                .eq("name",name)
                .eq("classify",classify)
                .between("price",pricemin,pricemax)
                .between("inventory",inventorymin,inventorymax)
                .eq("status",status);


        Page goodsPage = new Page(page,10);

        IPage<Goods> iPage = goodsMapper.selectMapsPage(goodsPage,objectQueryWrapper);
        System.out.println("总条数：" + iPage.getTotal());
        System.out.println("总页数：" + iPage.getPages());

        System.out.println("当前页的分页数据：" + iPage.getRecords());
        Map<List, Integer> map = new HashMap<>();
        map.put(iPage.getRecords(), iPage.getRecords().size());
        return map;
    }



}
